package com.qfedu.controller;

import com.qfedu.constants.Status;
import com.qfedu.model.Project;
import com.qfedu.model.Stage;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IStageService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/30 20:33
 */

@RestController
@RequestMapping("stage")
public class StageController {

    @Autowired
    private IStageService stageService;

    /**
     * @Author Tbh.
     * @Description: 工程阶段分页查询
     * @Date 2022/7/1 9:21
     * @Param QueryPageBean
     * @return PageResult
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = stageService.findPage(queryPage);
        return page;
    }

    /**
     * @Author Tbh.
     * @Description: 新增工程阶段
     * @Date 2022/7/1 10:22
     * @Param Stage
     * @return Result
     */
    @PostMapping("add")
    public Result addStage(@RequestBody Stage stage) {
        int row = stageService.addStage(stage);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 数据回显
     * @Date 2022/7/1 21:45
     * @Param sid
     * @return Result
     */
    @GetMapping("findBySid")
    public Result findByPid(@RequestParam("sid") String sid) {
        Stage stage = stageService.findBySid(sid);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(stage)) {
            result.setFlag(true);
            result.setData(stage);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;

    }

    /**
     * @Author Tbh.
     * @Description: 修改工程阶段
     * @Date 2022/7/1 21:50
     * @Param stage
     * @return Result
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Stage stage) {
        int row = stageService.edit(stage);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 逻辑删除
     * @Date 2022/7/1 21:59
     * @Param sid
     * @return Result
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("sid") String sid){
        int row =  stageService.deleteBySid(sid);
        if (row > 0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
